package ahblue.cn.tanzi.jmessage.im.view;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.support.annotation.Nullable;
import android.support.constraint.ConstraintLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.ArrayList;
import java.util.List;

import ahblue.cn.tanzi.R;
import ahblue.cn.tanzi.jmessage.im.ui.fragment.ConversationListFragment;
import ahblue.cn.tanzi.jmessage.im.ui.CreateGroupActivity;
import ahblue.cn.tanzi.jmessage.im.ui.FriendRecommendActivity;
import ahblue.cn.tanzi.jmessage.im.util.ThreadUtil;
import ahblue.cn.tanzi.model.CircleHead;
import ahblue.cn.tanzi.ui.msg.FriendlistActivity;

/**
 * 消息列表控件
 * Created by ${chenyn} on 2017/3/13.
 */

public class ConversationListView {
    private View mConvListFragment;
    private ListView mConvListView = null;
    private TextView mTitle;
    private ImageButton mCreateGroup;
    private LinearLayout mSearchHead;
    private LinearLayout mHeader;
    private RelativeLayout mLoadingHeader;
    private ImageView mLoadingIv;
    private LinearLayout mLoadingTv;
    private Context mContext;
    private RelativeLayout mNull_conversation;
    private FrameLayout mSearch;
    //    private TextView mAllUnReadMsg;
    private ConversationListFragment mFragment;
    private ConstraintLayout mThreeHead;
    private RecyclerView recyclerViewCircleList;

    public ConversationListView(View view, Context context, ConversationListFragment fragment) {
        this.mConvListFragment = view;
        this.mContext = context;
        this.mFragment = fragment;
    }

    public void initModule() {
        mConvListView = (ListView) mConvListFragment.findViewById(R.id.conv_list_view);
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mHeader = (LinearLayout) inflater.inflate(R.layout.conv_list_head_view, mConvListView, false);
        mSearchHead = (LinearLayout) inflater.inflate(R.layout.conversation_head_view, mConvListView, false);
        mLoadingHeader = (RelativeLayout) inflater.inflate(R.layout.jmui_drop_down_list_header, mConvListView, false);
        mLoadingIv = (ImageView) mLoadingHeader.findViewById(R.id.jmui_loading_img);
        mLoadingTv = (LinearLayout) mLoadingHeader.findViewById(R.id.loading_view);
        mSearch = (FrameLayout) mSearchHead.findViewById(R.id.search_title);
        mCreateGroup = (ImageButton) mSearchHead.findViewById(R.id.create_group_btn);
        mThreeHead = (ConstraintLayout) inflater.inflate(R.layout.conversation_three_head_view, mConvListView, false);
        recyclerViewCircleList = (RecyclerView) mThreeHead.findViewById(R.id.recyclerViewCircleList);
        List<CircleHead> circleHeads = new ArrayList<>();
        circleHeads.add(new CircleHead(1, "发起群聊", "", R.drawable.use_icon_news));
        circleHeads.add(new CircleHead(2, "好友列表", "", R.drawable.use_icon_order));
        circleHeads.add(new CircleHead(3, "新的朋友", "", R.drawable.deal_homepage_xiangmu_15));
        CircleHeadAdapter adapter = new CircleHeadAdapter(circleHeads);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (position == 0) {
//                    SystemMsgActivity.into((Activity) mContext);
                    Intent intent = new Intent(mFragment.getContext(), CreateGroupActivity.class);
                    mContext.startActivity(intent);
                } else if (position == 1) {
                    FriendlistActivity.into((Activity) mContext);
                } else {
//                    NewFriendActivity.into((Activity) mContext);
                    mContext.startActivity(new Intent(mContext,FriendRecommendActivity.class));
                }
            }
        });
        recyclerViewCircleList.setLayoutManager(new GridLayoutManager((Activity) mContext, 3));
        recyclerViewCircleList.setAdapter(adapter);
        mNull_conversation = (RelativeLayout) mConvListFragment.findViewById(R.id.null_conversation);
        // TODO 上层activity的阅读总数
//        mAllUnReadMsg = (TextView) mFragment.getActivity().findViewById(R.id.all_unread_number);
        mConvListView.addHeaderView(mSearchHead);
        mConvListView.addHeaderView(mLoadingHeader);
        mConvListView.addHeaderView(mThreeHead);
        mConvListView.addHeaderView(mHeader);
    }

    public void setConvListAdapter(ListAdapter adapter) {
        mConvListView.setAdapter(adapter);
    }


    public void setListener(View.OnClickListener onClickListener) {
        mSearch.setOnClickListener(onClickListener);
        mCreateGroup.setOnClickListener(onClickListener);
    }

    public void setItemListeners(AdapterView.OnItemClickListener onClickListener) {
        mConvListView.setOnItemClickListener(onClickListener);
    }

    public void setLongClickListener(AdapterView.OnItemLongClickListener listener) {
        mConvListView.setOnItemLongClickListener(listener);
    }


    public void showHeaderView() {
        mHeader.findViewById(R.id.network_disconnected_iv).setVisibility(View.VISIBLE);
        mHeader.findViewById(R.id.check_network_hit).setVisibility(View.VISIBLE);
    }

    public void dismissHeaderView() {
        mHeader.findViewById(R.id.network_disconnected_iv).setVisibility(View.GONE);
        mHeader.findViewById(R.id.check_network_hit).setVisibility(View.GONE);
    }


    public void showLoadingHeader() {
        mLoadingIv.setVisibility(View.VISIBLE);
        mLoadingTv.setVisibility(View.VISIBLE);
        AnimationDrawable drawable = (AnimationDrawable) mLoadingIv.getDrawable();
        drawable.start();
    }

    public void dismissLoadingHeader() {
        mLoadingIv.setVisibility(View.GONE);
        mLoadingTv.setVisibility(View.GONE);
    }

    public void setNullConversation(boolean isHaveConv) {
        if (isHaveConv) {
            mNull_conversation.setVisibility(View.GONE);
        } else {
            mNull_conversation.setVisibility(View.VISIBLE);
        }
    }


    public void setUnReadMsg(final int count) {
        ThreadUtil.runInUiThread(new Runnable() {
            @Override
            public void run() {
                // todo
//                if (mAllUnReadMsg != null) {
//                    if (count > 0) {
//                        mAllUnReadMsg.setVisibility(View.VISIBLE);
//                        if (count < 100) {
//                            mAllUnReadMsg.setText(count + "");
//                        } else {
//                            mAllUnReadMsg.setText("99+");
//                        }
//                    } else {
//                        mAllUnReadMsg.setVisibility(View.GONE);
//                    }
//                }
            }
        });
    }

    public class CircleHeadAdapter extends BaseQuickAdapter<CircleHead, BaseViewHolder> {

        public CircleHeadAdapter(@Nullable List<CircleHead> data) {
            super(R.layout.item_msg_circles, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, CircleHead item) {
            helper.setText(R.id.tv_circles_title, item.getTitle());
            helper.setImageResource(R.id.iv_circles_pic, item.imageResouceId);
        }
    }
}
